<template>
  <div style="color: black">
    <el-button @click="startDownload">开始下载</el-button>

    <!--    <el-button @click="sendSocket('xuanchuan')">信息宣传</el-button>-->
    <!--    <el-button @click="sendSocket('floor')">楼层指南</el-button>-->
    <!--    <el-button @click="sendSocket('screen')">全屏播放</el-button>-->
    <!--    <el-button @click="sendSocket('reload')">刷新数据</el-button>-->
    <div class="fucks">
      <MediaPlayer :list="mediaList"/>
    </div>
  </div>
</template>
<script>
import request, {pData} from "@/utils/http";
import MediaPlayer from "@/components/MediaPlayer.vue";
import _ from "lodash";

const {ipcRenderer} = require('electron');
export default {
  components: {MediaPlayer},
  mounted() {
    // this.startDownload()
    // this.setIpcEvent()
  },
  computed:{
    mediaList(){
      return this.$store.state.mediaList
    }
  },
  methods: {
    setIpcEvent(){
      ipcRenderer.once('willBeDownload', (event, message) => {
        console.log('远程和本地有差异，正在使用远程数据', message)
        this.formatMediaRemoteData(message)
      })
      ipcRenderer.on('allTheSame', (event, message) => {
        console.log('本地和远程全部一样，已启用本地加载', message)
        this.formatMediaLocalData(message)
      })
      ipcRenderer.on('netWorkTimeOut', (event, message) => {
        console.log('文件下载超时',message)
      })
      ipcRenderer.on('netWorkError', (event, message) => {
        this.$store.commit('setMediaList',message)
        console.log('没有网路，正在使用本地已有资源',message)
      })
    },
    startDownload() {
      ipcRenderer.send('startDownload');
    },
    formatMediaLocalData(message) {
      let tempData = message.map((s, i) => {
        let tempItem = message[i]
        tempItem.timer = s.timer || '5';
        let t = ''
        if (s.img) {
          tempItem.type = 'image'
          t = s.img.split('/')
        } else if (s.video) {
          tempItem.type = 'mp4'
          t = s.video.split('/')
        }
        tempItem.finallyUrl = '/media/' + t[t.length - 1]
        return tempItem
      })
      this.$store.commit('setMediaList',_.orderBy(tempData, ['ordernum'], 'asc'))
    },
    formatMediaRemoteData(data){
      let temp = []
      data.forEach((s, i) => {
        data[i].timer = s.timer || '5';
        if (s.img) {
          data[i].type = 'image'
          data[i].finallyUrl = s.img
        } else if (s.video) {
          data[i].type = 'mp4'
          data[i].finallyUrl = s.video
        }
        temp.push(s)
      })
      this.$store.commit('setMediaList',_.orderBy(temp, ['ordernum'], 'asc'))
    },
    sendSocket(mode) {
      request.post('/cmbc/api/pad/ic/devices/applyMode', {
        data: {
          brno: "0331",
          deviceId: "99",
          mode,
        }
      })
    }
  },

}
</script>
<style lang="scss" scoped>
.fucks {
  width: 400px;
  height: 400px;
  overflow: hidden;
}
</style>